import React, { Component } from 'react'

// 导入Spring组件
// import { Spring } from 'react-spring/renderprops'

import FilterTitle from '../FilterTitle'
import FilterPicker from '../FilterPicker'
import FilterMore from '../FilterMore'

// 导入自定义的axios
import { API } from '../../../../utils/api'


import styles from './index.module.css'

const titleSelectedStatus = {
  area: false,
  mode: false,
  price: false,
  more: false
}
const defaultSelectedStatus = titleSelectedStatus

const selectedValues = {
  area: ['area', 'null'],
  mode: ['null'],
  price: ['null'],
  more: []
}



export default class Filter extends Component {
  state = {
    // 按位运算实现高亮
    titleSelectedStatus,
    // 控制FilterMore 或 FilterPicker组件的显示/隐藏
    openType: '',
    // 所有筛选条件数据
    allFilterData: {},
    // 筛选条件的选中值
    selectedValues
  }

  componentDidMount() {
    // 获取到body
    this.htmlBody = document.body

    this.getFiltersData()
  }

  // 封装获取所有筛选条件的方法
  async getFiltersData() {
    // 获取当前城市ID值
    const { value } = JSON.parse(localStorage.getItem('hkzf_city'))
    const res = await API.get(`/houses/condition?id=${value}`)
    this.setState({
      allFilterData: res.data.body
    })
  }

  // 点击标题菜单实现高亮
  // 完整的功能实现需要后续组件的配合完成
  onTitleClick = type => {
    // 给body添加样式
    this.htmlBody.className = 'body-fixed'
    const { titleSelectedStatus, selectedValues } = this.state
    // 创建新的标题选中对象
    const newTitleSelectedStatus = { ...titleSelectedStatus }

    // 遍历标题选中对象
    Object.keys(titleSelectedStatus).forEach(key => {
      // key是四个标题的每一项

      // 判断是否为当前标题，是则设置为true
      if (key === type) {
        newTitleSelectedStatus[key] = true
        return
      }

      // 其他标题
      const selectedVal = selectedValues[key]
      if (key === 'area' && (selectedVal.length !== 2 || selectedVal[0] !== 'area')) {
        newTitleSelectedStatus[key] = true
      } else if (key === 'mode' && selectedVal[0] !== 'null') {
        newTitleSelectedStatus[key] = true
      } else if (key === 'price' && selectedVal[0] !== 'null') {
        newTitleSelectedStatus[key] = true
      } else if (key === 'more' && selectedVal.length !== 0) {
        // 更多选择项高亮，待定
        newTitleSelectedStatus[key] = true
      } else {
        // 当前不应该高亮
        newTitleSelectedStatus[key] = false
      }
    })
    // console.log(newTitleSelectedStatus)

    this.setState({
      // 跟新对话框标题高亮选中
      titleSelectedStatus: newTitleSelectedStatus,
      // 展示对话框
      openType: type
    })
  }

  // 取消 / 隐藏对话框
  onCancel = (type) => {
    // 给body删除样式
    this.htmlBody.className = ''
    // 菜单高亮逻辑处理
    const { titleSelectedStatus, selectedValues } = this.state
    // 创建新的标题选中对象
    const newTitleSelectedStatus = { ...titleSelectedStatus }
    const selectedVal = selectedValues[type]
    if (type === 'area' && (selectedVal.length !== 2 || selectedVal[0] !== 'area')) {
      newTitleSelectedStatus[type] = true
    } else if (type === 'mode' && selectedVal[0] !== 'null') {
      newTitleSelectedStatus[type] = true
    } else if (type === 'price' && selectedVal[0] !== 'null') {
      newTitleSelectedStatus[type] = true
    } else if (type === 'more' && selectedVal.length !== 0) {
      // 更多选择项高亮，待定
      newTitleSelectedStatus[type] = true
    } else {
      // 当前不应该高亮
      newTitleSelectedStatus[type] = false
    }

    this.setState({
      // 跟新对话框标题高亮选中
      titleSelectedStatus: newTitleSelectedStatus,
      openType: ''
    })
  }

  // 确定 / 隐藏对话框
  OnSave = (type, value) => {
    // 给body删除样式
    this.htmlBody.className = ''
    // 菜单高亮逻辑处理
    const { titleSelectedStatus } = this.state
    // 创建新的标题选中对象
    const newTitleSelectedStatus = { ...titleSelectedStatus }
    const selectedVal = value
    if (type === 'area' && (selectedVal.length !== 2 || selectedVal[0] !== 'area')) {
      newTitleSelectedStatus[type] = true
    } else if (type === 'mode' && selectedVal[0] !== 'null') {
      newTitleSelectedStatus[type] = true
    } else if (type === 'price' && selectedVal[0] !== 'null') {
      newTitleSelectedStatus[type] = true
    } else if (type === 'more' && selectedVal.length !== 0) {
      // 更多选择项高亮，待定
      newTitleSelectedStatus[type] = true
    } else {
      // 当前不应该高亮
      newTitleSelectedStatus[type] = false
    }

    const newSelectedValues = {
      ...this.state.selectedValues,
      // 只跟新当前type对应值
      [type]: value
    }
    // 最新选中值
    // console.log(newSelectedValues)

    const { area, mode, price, more } = newSelectedValues

    // 筛选条件数据
    const filters = {}

    // 区域
    const areaKey = area[0]
    let areaValue = 'null'
    if (area.length === 3) {
      areaValue = area[2] !== 'null' ? area[2] : area[1]
    }
    filters[areaKey] = areaValue

    // 方式和租金
    filters.mode = mode[0]
    filters.price = price[0]

    // 更多筛选条件
    filters.more = more.join(',')
    // console.log(filters)

    // 调用父组件的方法将筛选数据展示出来
    this.props.onFilters(filters)

    // 隐藏对话框
    this.setState({
      openType: '',

      // 跟新菜单高亮数据
      titleSelectedStatus: newTitleSelectedStatus,
      selectedValues: newSelectedValues
    })
  }

  // 渲染FilterPicker组件的方法
  renderFilterPickers() {
    const { openType, allFilterData: { area, subway, rentType, price }, selectedValues } = this.state

    if (openType !== 'area' && openType !== 'mode' && openType !== 'price') {
      return null
    }

    // 根据openType 获取当前筛选条件
    let data = []
    let cols = 3
    let defaultValue = selectedValues[openType]
    switch (openType) {
      case 'area':
        // 获取当前区域数据
        data = [area, subway]
        break;
      case 'mode':
        // 获取整租合租数据
        data = rentType
        cols = 1
        break
      case 'price':
        // 获取价格数据
        data = price
        cols = 1
        break
      default:
        break;
    }
    return <FilterPicker onCancel={this.onCancel} OnSave={this.OnSave} data={data} cols={cols} type={openType} defaultValue={defaultValue} key={openType} />
  }

  renderFilterMore() {
    const { openType, allFilterData: { roomType, oriented, floor, characteristic }, selectedValues } = this.state
    if (openType !== 'more') {
      return null
    }
    const data = {
      roomType, oriented, floor, characteristic
    }
    const defaultValues = selectedValues.more
    return <FilterMore data={data} OnSave={this.OnSave} type={openType} defaultValues={defaultValues} onCancel={this.onCancel} />
  }

  render() {
    const { titleSelectedStatus, openType } = this.state

    return (
      <div className={styles.root}>
        {/* 前三个菜单的遮罩层 */}
        {
          (openType === 'area' || openType === 'mode' || openType === 'price')
            ? <div className={styles.mask} onClick={() => this.onCancel(openType)}></div>
            : null
        }

        <div className={styles.content}>
          {/* 标题栏 */}
          <FilterTitle titleSelectedStatus={titleSelectedStatus} onClick={this.onTitleClick} />

          {/* 前三个菜单对应内容： */}
          {this.renderFilterPickers()}

          {/* 最后一个菜单对应内容 */}
          {this.renderFilterMore()}
        </div>
      </div>
    )
  }
}